package jinyilw.swing.rlaf.ui;

import jinyilw.swing.rlaf.theme.RThemeColor;
import jinyilw.swing.rlaf.utils.RBorderFactory.EmptyBorder;

import javax.swing.Icon;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.ListCellRenderer;
import javax.swing.border.Border;
import javax.swing.plaf.basic.BasicHTML;
import java.awt.Color;
import java.awt.Component;
import java.awt.Rectangle;
import java.io.Serializable;

/**
 * @author Jinyi
 */
public class RItemRenderer extends JLabel
		implements
		ListCellRenderer<Object>,
		Serializable
{
	private static final long serialVersionUID = 7057618122981294505L;
	private static final Border border = new EmptyBorder(2, 3, 2, 2);

	public RItemRenderer()
	{
		super();
		setBorder(border);
	}

	@Override
	public Component getListCellRendererComponent(JList list, Object value,
			int index, boolean isSelected, boolean cellHasFocus)
	{
		setFont(list.getFont());
		if (isSelected)
		{
			setOpaque(true);
			setForeground(RThemeColor.selectedTextColor);
			setBackground(RThemeColor.darkThemeColor);
		} else
		{
			setOpaque(false);
			setForeground(RThemeColor.colorSet.textColor);
		}

		if (value instanceof Icon)
		{
			setIcon((Icon) value);
			setText("");
		} else
		{
			setIcon(null);
			setText((value == null) ? "" : value.toString());
		}
		return this;
	}

	@Override
	public boolean isOpaque()
	{
		Color back = getBackground();
		Component p = getParent();
		if (p != null)
		{
			p = p.getParent();
		}
		boolean colorMatch = (back != null) && (p != null)
				&& back.equals(p.getBackground()) && p.isOpaque();
		return !colorMatch && super.isOpaque();
	}

	@Override
	public void validate()
	{
	}

	@Override
	public void invalidate()
	{
	}

	@Override
	public void repaint()
	{
	}

	@Override
	public void revalidate()
	{
	}

	@Override
	public void repaint(long tm, int x, int y, int width, int height)
	{
	}

	@Override
	public void repaint(Rectangle r)
	{
	}

	@Override
	protected void firePropertyChange(String propertyName, Object oldValue,
			Object newValue)
	{
		if (propertyName == "text"
				|| ((propertyName == "font" || propertyName == "foreground")
				&& oldValue != newValue
				&& getClientProperty(BasicHTML.propertyKey) != null))
		{

			super.firePropertyChange(propertyName, oldValue, newValue);
		}
	}

	@Override
	public void firePropertyChange(String propertyName, byte oldValue,
			byte newValue)
	{
	}

	@Override
	public void firePropertyChange(String propertyName, char oldValue,
			char newValue)
	{
	}

	@Override
	public void firePropertyChange(String propertyName, short oldValue,
			short newValue)
	{
	}

	@Override
	public void firePropertyChange(String propertyName, int oldValue,
			int newValue)
	{
	}

	@Override
	public void firePropertyChange(String propertyName, long oldValue,
			long newValue)
	{
	}

	@Override
	public void firePropertyChange(String propertyName, float oldValue,
			float newValue)
	{
	}

	@Override
	public void firePropertyChange(String propertyName, double oldValue,
			double newValue)
	{
	}

	@Override
	public void firePropertyChange(String propertyName, boolean oldValue,
			boolean newValue)
	{
	}

}